package cn.ai.boot.config.PCF;

import cn.ai.boot.PcmDataFieldConstant;
import org.apache.commons.collections4.MapUtils;
import org.apache.commons.lang3.StringUtils;

import java.io.IOException;
import java.util.*;

public class Main {
    public static void main(String[] args) throws IOException {
        PCFProcessor processor = new PCFProcessor("D:\\download\\testpcf\\x\\2170-BDG-122003.pcf");

        List<String> lines = processor.getLines();
        // 2 获取PCF文件，顶部 UNITS-BORE 的值
        String value = processor.getValueByKey("END-POINT");
        System.out.println("提取的值: " + value);

        // 3 判断整个PCF文件中，是否有 FABRICATION-ITEM的标识，如果有替换为ERECTION-ITEM
        processor.replaceAllIdentifiers("    FABRICATION-ITEM", "ERECTION-ITEM");

        // 功能3示例
        List<int[]> ranges = processor.getValueRanges2("0001388C-0000-0000-7615-5BA44A671D04");

        for (int[] range : ranges) {
            System.out.println("范围是" + range[0] + "," + range[1]);
        }
        // 查询该元件范围内的信息，把ERECTION-ITEM 替换为 FABRICATION-ITEM；
        processor.replaceIdentifiersInRange("ERECTION-ITEM", "FABRICATION-ITEM", ranges);

        // 在范围内末尾插入某行数据
        processor.insertLineAtRangeEnds("    NEWLY_INSERTED_LINE", ranges);

        // 在范围内头尾插入某行数据
        // processor.insertLinesAtHeaderNextAndEnd("    NEWLY_START", "    NEWLY_END", ranges);

        System.out.println("---------------------------------");
        for (int[] range : ranges) {
            System.out.println("范围是" + range[0] + "," + range[1]);
        }


        List<int[]> ranges2 = processor.getValueRanges2("000138AA-0000-0000-370F-4C30B466B00A");

        //更新PCF文件该范围内【SKEY】标识的值为“WW”；
        processor.updateKeyValueInRange("WELD-ATTRIBUTE3", "WssS", ranges);


        for (int[] range : ranges2) {
            System.out.println("重新获取的范围是" + range[0] + "," + range[1]);
        }


        int maxIdentifierByKey = processor.getMaxIdentifierByKey("COMPONENT-IDENTIFIER");
        System.out.println("最大值是：" + maxIdentifierByKey);

        List<Map<String, Object>> testWeldPipeData = createTestWeldPipeData();
        Map<String, PCFProcessor> pcfProcessorMap = new HashMap<>();
        pcfProcessorMap.put("test", processor);
        appendWeldSections(pcfProcessorMap, testWeldPipeData, maxIdentifierByKey);
        processor.saveToFile();
    }

    // 创建测试用的weldPipe数据
    private static List<Map<String, Object>> createTestWeldPipeData() {
        List<Map<String, Object>> weldPipe = new ArrayList<>();

        // 添加第一个焊口数据
        Map<String, Object> weld1 = new HashMap<>();
        weld1.put(PcmDataFieldConstant.PCM_COMPONENT_IDENTIFIER, "3");
        weld1.put(PcmDataFieldConstant.PCM_COORDINATE_1, "2564269.180 3030243.097 100877.648");
        weld1.put(PcmDataFieldConstant.PCM_DN, "100");
        weld1.put(PcmDataFieldConstant.PCM_NPS, "4");
        weld1.put(PcmDataFieldConstant.PCM_SKEY, "WS");
        weld1.put(PcmDataFieldConstant.PCM_WLDGING_ORD_NO, "2");
        weld1.put(PcmDataFieldConstant.PCM_UCI, "{000138AA-0000-0000-1F25-E8BF7265BC06}");
        weld1.put(PcmDataFieldConstant.PCM_PIPELINE_NUMBER, "3100-LS-3100C6001BUS");
        weld1.put(PcmDataFieldConstant.PCM_PREFAB_COMPONENTS_NO, "PREFAB001");

        // 添加第二个焊口数据
        Map<String, Object> weld2 = new HashMap<>();
        weld2.put(PcmDataFieldConstant.PCM_COMPONENT_IDENTIFIER, "5");
        weld2.put(PcmDataFieldConstant.PCM_COORDINATE_1, "2564270.000 3030244.000 100878.000");
        weld2.put(PcmDataFieldConstant.PCM_DN, "150");
        weld2.put(PcmDataFieldConstant.PCM_NPS, "6");
        weld2.put(PcmDataFieldConstant.PCM_SKEY, "WT");
        weld2.put(PcmDataFieldConstant.PCM_WLDGING_ORD_NO, "3");
        weld2.put(PcmDataFieldConstant.PCM_UCI, "{000138AA-0000-0000-1F25-E8BF7265BC07}");
        weld2.put(PcmDataFieldConstant.PCM_PIPELINE_NUMBER, "3100-LS-3100C6002BUS");
        // 不设置预制件编号，用于测试现场焊口

        weldPipe.add(weld1);
        weldPipe.add(weld2);

        return weldPipe;
    }

    // WELD段模板常量
    private static final String WELD_SECTION_TEMPLATE =
            "WELD\n" +
                    "    COMPONENT-IDENTIFIER %s\n" +
                    "    MASTER-COMPONENT-IDENTIFIER %s\n" +
                    "    END-POINT %s\n" +
                    "    END-POINT %s\n" +
                    "    SKEY %s\n" +
                    "    REPEAT-WELD-IDENTIFIER %s\n" +
                    "    UCI %s\n" +
                    "    WELD-REMARK-NUMBER \n" +
                    "    WELD-ATTRIBUTE1 %s\n" +
                    "    WELD-ATTRIBUTE2 %s\n" +
                    "    WELD-ATTRIBUTE3 %s\n" +
                    "    WELD-ATTRIBUTE4 %s\n";

    // 在 genBlueprintTask 方法中使用模板
    // 在处理完现有逻辑后，添加以下代码段：

    /**
     * 追加WELD段信息到PCF文件
     */
    public static void appendWeldSections(Map<String, PCFProcessor> pcfProcessorMap,
                                          List<Map<String, Object>> weldPipe,
                                          int maxIdentifierByKey) {
        for (Map.Entry<String, PCFProcessor> entry : pcfProcessorMap.entrySet()) {
            String docId = entry.getKey();
            PCFProcessor processor = entry.getValue();
            // 获取UNITS-BORE值以确定单位制
            String unitsBore = processor.getUnitsBore();

            // 为每个直管焊口生成WELD段
            for (int i = 0; i < weldPipe.size(); i++) {
                Map<String, Object> weldData = weldPipe.get(i);

                // 构建WELD段内容
                String weldSection = buildWeldSection(weldData, unitsBore, maxIdentifierByKey + i + 1);

                // 将WELD段追加到PCF文件中
                processor.appendLinesToFileEnd(Arrays.asList(weldSection));
            }
        }
    }

    /**
     * 构建单个WELD段内容
     */
    private static String buildWeldSection(Map<String, Object> weldData, String unitsBore, int componentIdentifier) {
        // MASTER-COMPONENT-IDENTIFIER
        String masterComponentId = MapUtils.getString(weldData, PcmDataFieldConstant.PCM_COMPONENT_IDENTIFIER, StringUtils.EMPTY);

        // 两个END-POINT
        String endPoint1 = buildEndPoint(weldData, unitsBore);
        String endPoint2 = buildEndPoint(weldData, unitsBore);

        // SKEY
        String skey = MapUtils.getString(weldData, PcmDataFieldConstant.PCM_SKEY, "WELD");

        // REPEAT-WELD-IDENTIFIER
        String repeatWeldIdentifier = MapUtils.getString(weldData, PcmDataFieldConstant.PCM_WLDGING_ORD_NO, StringUtils.EMPTY);

        // UCI
        String uci = MapUtils.getString(weldData, PcmDataFieldConstant.PCM_UCI, StringUtils.EMPTY);

        // WELD-ATTRIBUTE1
        String weldAttribute1 = MapUtils.getString(weldData, PcmDataFieldConstant.PCM_PIPELINE_NUMBER, StringUtils.EMPTY);

        // WELD-ATTRIBUTE2
        String weldAttribute2 = getDnOrNpsValue(weldData, unitsBore);

        // WELD-ATTRIBUTE3 和 WELD-ATTRIBUTE4 (根据预制件编号判断)
        String prefabNo = MapUtils.getString(weldData, PcmDataFieldConstant.PCM_PREFAB_COMPONENTS_NO, StringUtils.EMPTY);
        String weldAttribute3 = determineWeldAttribute3(prefabNo);
        String weldAttribute4 = determineWeldAttribute4(prefabNo);

        // 使用模板格式化
        return String.format(WELD_SECTION_TEMPLATE,
                componentIdentifier,
                masterComponentId,
                endPoint1,
                endPoint2,
                skey,
                repeatWeldIdentifier,
                uci,
                weldAttribute1,
                weldAttribute2,
                weldAttribute3,
                weldAttribute4);
    }

    /**
     * 构建END-POINT值
     */
    private static String buildEndPoint(Map<String, Object> weldData, String unitsBore) {
        String coordinate = MapUtils.getString(weldData, PcmDataFieldConstant.PCM_COORDINATE_1, StringUtils.EMPTY);
        String dnOrNps = getDnOrNpsValue(weldData, unitsBore);

        // 5个空格分隔
        return coordinate + "     " + dnOrNps;
    }

    /**
     * 根据单位制获取DN或NPS值
     */
    private static String getDnOrNpsValue(Map<String, Object> data, String unitsBore) {
        String value;
        if ("INCH".equals(unitsBore)) {
            // 英制取NPS
            value = MapUtils.getString(data, PcmDataFieldConstant.PCM_NPS, StringUtils.EMPTY);
            // 如果NPS为空，则取DN
            if (StringUtils.isEmpty(value)) {
                value = MapUtils.getString(data, PcmDataFieldConstant.PCM_DN, StringUtils.EMPTY);
            }
        } else {
            // 公制取DN
            value = MapUtils.getString(data, PcmDataFieldConstant.PCM_DN, StringUtils.EMPTY);
            // 如果DN为空，则取NPS
            if (StringUtils.isEmpty(value)) {
                value = MapUtils.getString(data, PcmDataFieldConstant.PCM_NPS, StringUtils.EMPTY);
            }
        }
        return value;
    }

    /**
     * 根据预制件编号确定WELD-ATTRIBUTE3值
     */
    private static String determineWeldAttribute3(String prefabNo) {
        // 根据业务逻辑确定值，示例：
        if (StringUtils.isNotEmpty(prefabNo)) {
            return "Shop weld"; // 有预制件编号时为工厂焊口
        }
        return "Field weld"; // 无预制件编号时为现场焊口
    }

    /**
     * 根据预制件编号确定WELD-ATTRIBUTE4值
     */
    private static String determineWeldAttribute4(String prefabNo) {
        // 根据业务逻辑确定值，示例：
        if (StringUtils.isNotEmpty(prefabNo)) {
            return "Fabricator"; // 有预制件编号时为预制厂
        }
        return "Contractor"; // 无预制件编号时为承包商
    }
// 2 获取PCF文件，顶部 UNITS-BORE 的值

    // 6点测试
    private static void test6() throws IOException {
        /**
         * 更新PCF文件该范围内【SKEY】标识的值为“WW”；
         *
         * 更新PCF文件该范围内【WELD-ATTRIBUTE3】标识的值为“Shop weld”；
         *
         * 更新PCF文件该范围内【WELD-ATTRIBUTE4】标识的值为“Fabricator”；
         */
    }
}
